package com.qf.controller;

import com.qf.entity.Student;
import com.qf.entity.StudentVO;
import com.qf.service.StudentService;
import com.qf.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@CrossOrigin
public class StudentController {
    @Autowired
    private StudentService studentService;

    //根据id查询学生信息
    @RequestMapping("/selectById")
    public R selectById(int id) {
         return R.success(studentService.findById(id));
    }

    //查询所有学生信息
    @RequestMapping("/selectAll")
    public R selectAll() {
        return R.success(studentService.selectAll());
    }


    //模糊查询(条件查询)
    @RequestMapping("/findByKeyword")
    R findByKeyword( @RequestParam HashMap<String, Object> map) {
        System.out.println("map = " + map);
        return R.success(studentService.findByKeyword(map));
    }

    //添加学生信息
    @RequestMapping("/addStudent")
    public R addStudent(Student student) {
        System.out.println("student = " + student);
        return R.success(studentService.addStudent(student));
    }

    //删除学生信息
    @RequestMapping("/deleteStudent")
    public R deleteStudent(@RequestParam HashMap<String, Object> map) {
        System.out.println("map = " + map);
        return R.success(studentService.deleteStudent(map));
    }

    //修改学生信息
    @RequestMapping("/updateStudent")
    public R updateStudent(@RequestParam HashMap<String, Object> map) {
        System.out.println("map = " + map);
        return R.success(studentService.updateStudent(map));
    }


    //批量删除
    @RequestMapping("/batchDelete")
    public R batchDelete(@RequestParam List<Integer> ids) {
        System.out.println("ids = " + ids);
        return R.success(studentService.batchDeleteStudent(ids));
    }


    //查询所有学生信息以及关联的班级信息
    @RequestMapping("/selectAllVO")
    public R selectAllVO() {
        List<StudentVO> list = studentService.findAllStudentAndClassroom();
        return R.success(list);
    }


    //1对多查询
    //查询一个学生信息以及他所学习的所有学科信息
    @GetMapping("/subjects")
    public R findStudentAndAllSubjectByStuid(int id){
        StudentVO studentVO = studentService.findStudentAndAllSubjectByStuid(id);
        return R.success(studentVO);
    }


    //一对多对一（三表）
    //查询一个学生信息以及他所学习的所有学科信息,以及每个学科的老师信息
    @GetMapping("/subjects/teacher")
    public R findStudentAndAllSubjectAndTeacherByStuid(int id){
        StudentVO studentVO = studentService.findStudentAndAllSubjectAndTeacherByStuid(id);
        return R.success(studentVO);
    }


//    将一对一多表查询拆解成两个单表
    /**
     * 学生 --> 教室一对一查询,V2
     * @return
     */
    @GetMapping("/class/v2")
    public R findAllStudentAndClassroomV2(){
        List<StudentVO> list = studentService.findAllStudentAndClassroomV2( );
        return R.success(list);
    }

    //将一对多多表查询拆开
    /**
     * 一对多演示,1个学生对应多个学科信息V2
     * @param id
     * @return
     */
    @GetMapping("/subjects/v2")
    public R findStudentAndAllSubjectByStuidV2(int id){
        StudentVO studentVO = studentService.findStudentAndAllSubjectByStuidV2(id);
        return R.success(studentVO);
    }


    //登录请求
    @GetMapping("/login")
    public R login(@RequestParam HashMap<String, Object> map){
    // 查询数据库
        Student student = studentService.findStudentByUsername(map);
        return R.success(student);
    }



}
